<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of RepFacturaDetallada
 *
 * @author Bryan Cruz
 */
class RepFacturaDiaria extends ReportBase {
    
    public $where = NULL;
    public $stud = "";

    function __construct() {
        $this->callbackSql = "cbSqlGenerador"; //funcion que genera sql
        $this->callbackRow = "funcRows"; //funcion para cada fila generada
        $this->paramPage = "rpt_facturaDiaria_param.php";
        $this->resultPage = "rpt_facturaDiaria_resultado_1_1.php";
        $this->rootPath = RPT_PATH + "include";
        $this->label = "Facturacion Diaria";
        $this->subtitle = $this->label;
    }

    public function funcRows() {
        $newResult = array();
        foreach ($this->lastResult as $r) {
            $sql = "select * from cllg_factura";
            $rs = $this->query($sql);
            
        }
        $this->lastResult = $newResult;
    }

    /**
     * Genera el sql para el reporte
     */
     public function cbSqlGenerador() {
        #EaPdfPrinter::$ORIENTACION = "landscape";
        #$user = AppSecurity::$UserData;
        #$user = $user["data"];
           
        $sql = "SELECT * FROM cllg_factura ";
        $w = "";
 
        if(isEmpty(getParam("allFechas")))
            if (!isEmpty(getParam("desde")) && !isEmpty(getParam("hasta"))) {
                $w.=sprintf("cllg_factura.FECHA BETWEEN '%s' AND '%s'", getParam("desde"), getParam("hasta"));
            }
        if (!isEmpty(getParam("tipoPago"))) {
            $w = isEmpty($w) ? $w : "$w and ";
            $w.=sprintf("cllg_factura.%s > 0 ", getParam("tipoPago"));
        }
        if (!isEmpty(getParam("numBoleta"))) {
            $w = isEmpty($w) ? $w : "$w and ";
            $w.=sprintf("cllg_factura.NO_BOLETA like '%s'", getParam("numBoleta"));
        }
        if(!isEmpty(getParam("carnet"))){
            $w2 = "";
            
            $value = getParam("carnet");
            $pieces = explode(" - ", $value);
            $id = $pieces[0];
            
            $empData = $this->db->query_select("cllg_student","ID=$id");
            
            if($empData[0]['FK_EMPRESA'] != NULL){
                $emp = $empData[0]['FK_EMPRESA'];
                $w2 = "ID IN(
                        SELECT DISTINCT FK_FACTURA
                        FROM cllg_mov_cargo
                        WHERE FK_CARGO IN(
                            SELECT ID
                            FROM cllg_cargo
                            WHERE FK_ACCOUNT IN(
                                SELECT ID
                                FROM cllg_account
                                WHERE FK_STUDENT=$id)))
                       AND FK_EMPRESA=$emp";
                $w2 = isEmpty($w) ? $w2 : "$w2 AND $w";
                $w2.=" UNION
                       SELECT * 
                       FROM cllg_factura
                       WHERE FK_STUDENT=$id";
                $w2= isEmpty($w) ? $w2 : "$w2 AND $w";
            }else{
                $w2 = "FK_STUDENT=$id";
                $w2 = isEmpty($w) ? $w2 : "$w2 AND $w";
            }
        }
        
        if(isEmpty(getParam("allFechas"))){
            if (isEmpty($w)) {
                throw new Exception("<h5 style='color:blue;'>Seleccione fechas para generar reporte!<h5>");
            }
        }
        
        $this->lastQuery = isEmpty($w) && isEmpty($w2) ? $sql : (isEmpty(getParam("carnet")) ? ("$sql WHERE $w ORDER BY ID") : ("$sql WHERE $w2 ORDER BY ID"));
        $this->where = isEmpty($w) ? NULL : $w;
    }
    
    public function getStudent(){
         if (!isEmpty(getParam('carnet'))){
            $value = getParam("carnet");
            $pieces = explode(" - ", $value);
            $id = $pieces[0];
            
            $stud = $this->db->query_select("cllg_student",sprintf("ID=%s",  $id));
            if (count($stud) == 0){
                throw new Exception("<h5 style='color:blue'>No se encontró ningún alumno con ese carnet!</h5>");
                return false;
            }
            else{
                $stud = $stud[0];
                $this->stud = $stud['FIRST_NAME']. " " . $stud['LAST_NAME'];
                }
            }
         
    }
    
    public function generateSQL() {
        $this->getStudent();
        $this->cbSqlGenerador();
        $sqlRs = $this->db->queryToArray($this->lastQuery);
        $this->lastResult = $sqlRs;
    }
    
    public function totalCol($col,$table,$whereAdd = NULL){
       $sql = sprintf("SELECT SUM(%s) AS SUMA FROM %s",$col,$table);
       $w = "";
       if($this->where != NULL){
           $w = $this->where;
           $w.= "AND STATUS!=0";
       }
       if($whereAdd != NULL)
            if(!isEmpty($w)){
                $w = "$w AND ";
                $w.= $whereAdd;    
       }
       $res = isEmpty($w) ? $this->db->queryToArray("$sql WHERE STATUS!=0") : $this->db->queryToArray("$sql WHERE $w");
       return $res[0]['SUMA'];
    }
    
    public function getTasaProm($col,$table,$union = NULL){
       $sql = sprintf("SELECT AVG(%s) AS PROMEDIO FROM %s",$col,$table);
       $w = "";
       if($this->where != NULL){
           $w = $this->where;
           $w.= "AND STATUS!=0";
       }
       if($union != NULL)
            if(!isEmpty($w)){
                $w = "$w UNION ";
                $w.= $union;    
       }
       $res = isEmpty($w) ? $this->db->queryToArray("$sql WHERE NOT ISNULL(TASA_CAMBIO) AND TASA_CAMBIO!=0 AND STATUS!=0") : $this->db->queryToArray("$sql WHERE $w AND NOT ISNULL(TASA_CAMBIO) AND TASA_CAMBIO!=0");
       return $res[0]['PROMEDIO'];
    }
    
    public function noData($sql){
        if(count($sql)== 0){
                    echo "<tr>
                            <td colspan='13'>
                            NO SE ENCONTRARON DATOS
                            <td>
                          </tr>
                       </tbody>
                   </table>";
            return false;
        }
    }
    
    public function contains($str, $content) {
    return strlen(strstr($str,$content)) > 0;
    }
    
    public function getTypeaheadData($select,$table){
        $as = "DATA";
        $sqlData = sprintf("SELECT %s AS %s FROM %s",$select,$as,$table);
        $data = $this->db->queryToArray($sqlData);
        $string = "";

        foreach($data as $d){
            $string = isEmpty($string) ? $string : "$string,";
            $string.= '"'.$d[$as].'"';
        }
        $typeahead = "[". $string ."]";
        return $typeahead;
    }
}

?>
